<template>
    <div>

    </div>
</template>

<script>
    /*空白页,用于处理微信授权*/
    import {isDev, getQueryStringArgs} from "@/utils/tools";
    import Cookies from "js-cookie"

    export default {
        name: "Author",
        mounted() {
            const code = this.getUrlParam('code')
            console.log('code', code);
            let url = this.$store.state.app.toUrl;
            console.log('url', url);
            if (!code) {
                this.$api.user.author().then(res => {
                    //console.log('redirect_url',res.data)
                    let redirect_url = res.data
                    console.log(redirect_url)
                    redirect_url = /http\S*t/g.exec(redirect_url)
                    redirect_url = redirect_url[0].replace(/\\u0026/g, '&')
                    window.location.href = redirect_url
                }).catch(error => {
                    console.log(error);
                })
            } else {
                if (this.$store.state.app.toUrl) {
                    console.log(this.$store.state.app.toUrl)
                }
                this.$api.user.userInfo({"code": code}).then(res => {
                    console.log("userInfo", res.data)
                    let userInfo = res.data.userInfo
                    let isExisted = res.data.isExisted
                    this.$store.commit("setUserInfo", userInfo)
                    Cookies.set('token', userInfo.openId, {expires: 7});
                    Cookies.set('userInfoExisteTime', true, {expires: new Date(new Date().getTime() + 5 * 60 * 1000)});
                    console.log(userInfo)
                    if (isExisted) { // 已经关注/授权
                        if (this.$store.state.app.toUrl) {
                            //window.location.href = this.$store.state.app.toUrl
                            this.$router.push(this.$store.state.app.toUrl)
                            return
                        }
                        window.location.href = "/"
                    } else {
                        if(url == '/parking_register'){
                            window.location.href = "/#/parking_register"  //跳转停车场注册页面
                        }else{
                            window.location.href = "/#/register"  //跳转注册页面
                        }
                    }
                }).catch(error => {
                    console.log(error);
                })
            }
        },
        methods: {
            getUrlParam(parame) {
                let qs = getQueryStringArgs();
                return qs[parame]
            }
        }
    }
</script>

<style scoped>

</style>
